package com.wangfugui.mybatisplus.utils;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

public class DateUtil {
    public static final String F_YEAR_MM_DD = "yyyy-MM-dd";
    public static final String F_YEAR_MM_DD_H_M_S = "yyyy-MM-dd HH:mm:ss";
    public static final String F_YEARMMDDHMS = "yyyyMMddHHmmss";
    public static final String F_YEARMMDD = "yyyyMMdd";
    public static final String F_YEAR_MM_DD_H_M = "yyyy-MM-dd HH:mm";
    public static final String F_YEAR = "yyyy";
    public static final String F_MEM_NO = "yyyyMMddHHmms";

    public final static DateTimeFormatter FMT_NORMAL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter FMT_MEM_NO = DateTimeFormatter.ofPattern(F_MEM_NO);

    private static HashMap<String, SimpleDateFormat> formatHash = new HashMap<>();
    public static Date paseDate(String format, String date) {
        try {
            if(!formatHash.containsKey(format)){
                formatHash.put(format, new SimpleDateFormat(format));
            }
            return formatHash.get(format).parse(date);
        } catch (Exception e) {
        }
        return null;
    }

    public static String formatDate(String format, Date date) {
        if(!formatHash.containsKey(format)){
            formatHash.put(format, new SimpleDateFormat(format));
        }
        return formatHash.get(format).format(date);
    }
    public static String formatDate(Date date) {
        return formatDate(F_YEAR_MM_DD_H_M_S, date);
    }
    public static String formatDate(String format) {
        return formatDate(format, new Date());
    }

}